import api from '../../utils/api'
// pages/mine/mine.js
import {
  throttle
} from '../../utils/util'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appointmentList: [{
        number: 0,
        image: '../../assets/image/mine/appointment_pay.png',
        text: '待付款',
        type: 'waitPay'
      },
      {
        number: 0,
        image: '../../assets/image/mine/appointment_regist.png',
        text: '待登记',
        type: 'waitBookCount'
      },
      {
        number: 0,
        image: '../../assets/image/mine/appointment_order.png',
        text: '待预约',
        type: 'waitInjectCount'
      },
      {
        number: 0,
        image: '../../assets/image/mine/appointment_assets.png',
        text: '待评价',
        type: 'waitCommentCount'
      }
    ],
    severList: [{
        image: '../../assets/image/mine/report.png',
        text: '查看报告',
        type: 'report'
      },
      {
        image: '../../assets/image/mine/crm.png',
        text: '客服热线',
        type: 'crm'
      },
    ],
    mesList: [{
      image: '../../assets/image/mine/setting.png',
      text: '设置',
      type: 'setting'
    }],

    userInfo: {},
    needLogin: true,
    orderCount: {},
    mobile: '',
    showPhone: false,
    dataList: [],
    bindMobile: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  onShow() {
    wx.removeStorageSync('appointmentIndex')
    console.log(wx.getStorageSync('mobile'), '=============================')
    this.setData({
      needLogin: wx.getStorageSync('token') === '' ? true : false,
      userInfo: wx.getStorageSync('userInfo'),
      mobile: wx.getStorageSync('mobile'),
      bindMobile: wx.getStorageSync('bindMobile')
    })
    if (wx.getStorageSync('token')) {
      this.getVaccineOrderStatusCount()
    }
    this.getBannerList();
    this.getShowPrivacy();
  },

  onHide() {
    this.setData({
      privacyData: {}
    })
  },

  moveTo(e) {
    let index = e.currentTarget.dataset.index
    if (index === 3) {
      index = 4
    }
    throttle(function (e) {
      wx.navigateTo({
        url: `../appointment/appointment?index=${index}`
      })
    }, 2000)
  },
  moveToVaccine(e) {
    console.log(e.currentTarget.dataset.item)
    let {
      source,
      orderNo,
      healthExam
    } = e.currentTarget.dataset.item
    if (source && orderNo) {
      throttle(function (e) {
        wx.navigateTo({
          url: `../registeredVaccine/registeredVaccine?source=${source}&orderNo=${orderNo}&healthExam=${healthExam}`
        })
      }, 2000)
    }

  },
  moveToAppraise(e) {
    console.log(e.currentTarget.dataset.item)
    let {
      id
    } = e.currentTarget.dataset.item
    app.globalData.userAppraise = e.currentTarget.dataset.item
    if (id) {
      throttle(function (e) {
        wx.navigateTo({
          url: `../userAppraise/userAppraise?vaccineOrderId=${id}`
        })
      }, 2000)
    }

  },
  closeModal() {
    this.setData({
      // needLogin: false,
      // userInfo: wx.getStorageSync('userInfo'),
      // bindMobile: true
      needLogin: wx.getStorageSync('token') === '' ? true : false,
      userInfo: wx.getStorageSync('userInfo'),
      mobile: wx.getStorageSync('mobile'),
      bindMobile: wx.getStorageSync('bindMobile')
    })
    this.getVaccineOrderStatusCount()
  },
  getVaccineOrderStatusCount() {
    api.getVaccineOrderStatusCount({
      data: {
        mobile: wx.getStorageSync('mobile')
      },
      success: res => {
        if (res.code * 1 === 0) {
          let appointmentList = this.data.appointmentList
          for (let key in res.data) {
            appointmentList.forEach(item => {
              if (item.type === key) {
                item.number = res.data[key]
              }
            })
          }
          this.setData({
            appointmentList: appointmentList
          }, () => {
            this.getOrderStatusCount()
          })
        } else {
          wx.clearStorageSync()
        }
      }
    })
  },
  getOrderStatusCount() {
    const that = this
    api.getOrderStatusCount({
      data: {
        orderType: 7
      },
      success: res => {
        if (res.code * 1 === 0) {
          let waitPayCount = res.data.waitPayCount
          let appointmentList = that.data.appointmentList
          appointmentList[0]['number'] = waitPayCount
          that.setData({
            appointmentList: appointmentList
          })
        }
      }
    })
  },
  serveMoveTo(e) {
    let item = e.currentTarget.dataset.item

    let {
      type
    } = item
    console.log(type)
    if (type === 'report') {
      throttle(function (e) {
        wx.navigateTo({
          url: `../medicalReport/medicalReport`
        })
      }, 2000)
    }
    if (type === 'crm') {
      this.setData({
        showPhone: true
      })
    }
    if (type === 'setting') {
      throttle(function (e) {
        wx.navigateTo({
          url: `../setting/setting`
        })
      }, 2000)
    }
  },
  cancelDialog() {
    this.setData({
      showPhone: false
    })
  },
  async getBannerList() {
    let list = await this.orderFrontVaccineOrderListV3()
    let appraiseList = await this.appraiseList()
    let needList = appraiseList.filter((item) => {
      return !item.hasComment
    });
    this.setData({
      dataList: [...list, ...needList]
    })
  },
  appraiseList() {
    return new Promise((resolve, reject) => {
      api.orderFrontVaccineOrderListV3({
        data: {
          pageSize: 100,
          pageNum: 1,
          mobile: wx.getStorageSync('mobile'),
          status: 4,
          hasComment: false
        },
        success: res => {
          if (res.code * 1 === 0) {
            let dataList = res.data.dataList
            // console.log(dataList, 'dataList===============')
            // return dataList
            resolve(dataList)
          }
        }
      })
    })
  },
  orderFrontVaccineOrderListV3() {
    return new Promise((resolve, reject) => {
      api.orderFrontVaccineOrderListV3({
        data: {
          pageSize: 100,
          pageNum: 1,
          mobile: wx.getStorageSync('mobile'),
          status: 1
        },
        success: res => {
          if (res.code * 1 === 0) {
            let dataList = res.data.dataList
            // console.log(dataList, 'dataList===============')
            // return dataList
            resolve(dataList)
          }
        }
      })
    })

  },
  move(e) {
    let id = e.currentTarget.dataset.item.id
    throttle(function (e) {
      wx.navigateTo({
        url: `../orderMessage/orderMessage?id=${id}&&type=2`
      })
    }, 2000)
  },
  getShowPrivacy() {
    api.showPrivacy({
      data: {
        source: 2, // 1、app    2、小程序
      },
      success: (res) => {
        if (+res.code === 0) {
          this.setData({
            privacyData: res.data
          })
        }
      }
    })
  },

  onClosePrivacy() {
    const {
      privacyData
    } = this.data || {};
    privacyData.show = false
    this.setData({
      privacyData
    })
  },

})